package com.gopay.common.cps.dao.wallet.activity.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cps.dao.wallet.activity.WaActivityCountDao;
import com.gopay.common.domain.dps.WaActivityCount;
import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.common.util.DateUtils;

/**
 * @author ZHOUPENG
 * @date   2017年8月17日 上午11:59:13
 */
@Repository("waActivityCountDao")
public class WaActivityCountDaoImpl extends BaseDAOImpl<WaActivityCount, Serializable> implements WaActivityCountDao {

	@Resource(name = "commonQueryDAO")
	private CommonQueryDAO commonQueryDAO;
	
	@Override
	public List<WaActivityCount> getActivityCount(Map<String, Object> params) {
		String hql = "from WaActivityCount t where 1=1";
		for( Map.Entry<String, Object> entry : params.entrySet() ){
			hql += " AND "+entry.getKey()+"=:"+entry.getKey();
		}
		return this.findByHql(hql, params);
	}

	@Override
	public List<Object[]> getActivityMessage(String custId) {
		StringBuffer sb = new StringBuffer();
		sb.append(" select m.activity_id,")
		.append(" c.activityNum,")
		.append(" m.activity_type,")
		.append(" m.no_frequency_img_address,")
		.append(" m.no_frequency_img_url,")
		.append(" m.frequency_img_address,")
		.append(" m.frequency_img_url,")
		.append(" to_char(m.ACTIVITY_START_TIME,'yyyy-MM-dd') ACTIVITY_START_TIME,")
		.append(" to_char(m.ACTIVITY_END_TIME,'yyyy-MM-dd') ACTIVITY_END_TIME") 
		.append(" from wa_activity_manage m  ")
		.append(" left join ")
		.append(" (select c.activity_id, count(c.activity_count_id) activityNum ")
		.append(" from wa_activity_count c ")
		.append(" where c.receive_flag =:receiveFlag and c.cust_id=:custId  group by c.activity_id) c")
		.append(" on c.activity_id = m.activity_id")
		.append(" where 1 = 1")
		.append(" and m.activity_start_time <=:currtentDate ")
		.append(" and m.activity_end_time >=:currtentDate");
		Map<String,Object> map = new HashMap<String,Object>();
		Date currtDate=new Date();
		map.put("custId",custId);
		map.put("currtentDate",currtDate);
		//未玩过的
		map.put("receiveFlag",0);
		List<Object[]> activityList= commonQueryDAO.findBySql(sb.toString(), map);
		return activityList.size()==0?null:activityList;
	}
	
}
